﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess
;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IFlowProcessMessageService))]
    internal class FlowProcessMessageService : IFlowProcessMessageService
    {
        [Import]
        protected IFlowProcessMessageRepository FlowProcessMessageRepository { get; set; }

        public List<FlowProcessMessageView> GetFlowProcessMessage(string userName)
        {
            List<FlowProcessMessageView> flowProcessMessageViewList = new List<FlowProcessMessageView>();
            IQueryable<FlowProcessMessage> flowProcessMessageList = FlowProcessMessageRepository.Entities.Where(f => f.ProcessUser == userName && f.State == 0 && (f.ProcessType == "3" || f.ProcessType == "4" || f.ProcessType == "5"));
            foreach (FlowProcessMessage flowProcessMessage in flowProcessMessageList)
            {
                flowProcessMessageViewList.Add(new FlowProcessMessageView()
                {
                    ID = flowProcessMessage.ID,
                    RecordID = flowProcessMessage.RecordID,
                    FlowCode = flowProcessMessage.FlowCode,
                    SponsorUser = flowProcessMessage.SponsorUser,
                    ProcessType = flowProcessMessage.ProcessType,
                    LastUpdateTime = flowProcessMessage.LastUpdateTime,
                    AddDate = flowProcessMessage.AddDate
                });
            }
            return flowProcessMessageViewList;
        }

        public List<FlowProcessMessageView> GetFlowProcessMessageForBaklogTask(string userName)
        {
            List<FlowProcessMessageView> flowProcessMessageViewList = new List<FlowProcessMessageView>();
            IQueryable<FlowProcessMessage> flowProcessMessageList = FlowProcessMessageRepository.Entities.Where(f => f.State == 0 && f.ProcessUser == userName && f.ProcessType == "1");
            foreach (FlowProcessMessage flowProcessMessage in flowProcessMessageList)
            {
                flowProcessMessageViewList.Add(new FlowProcessMessageView()
                {
                    ID = flowProcessMessage.ID,
                    RecordID = flowProcessMessage.RecordID,
                    FlowCode = flowProcessMessage.FlowCode,
                    FlowNodeCode = flowProcessMessage.FlowNodeCode,
                    ProcessUserDepartment = flowProcessMessage.ProcessUserDepartment,
                    ProcessUser = flowProcessMessage.ProcessUser,
                    SponsorUser = flowProcessMessage.SponsorUser,
                    ProcessType = flowProcessMessage.ProcessType,
                    LastUpdateUser = flowProcessMessage.LastUpdateUser,
                    LastUpdateTime = flowProcessMessage.LastUpdateTime,
                    AddDate = flowProcessMessage.AddDate
                });
            }
            return flowProcessMessageViewList;
        }

        public List<FlowProcessMessageView> GetFlowProcessMessageForProcessTask(string userName)
        {
            string contrainUserName = "," + userName + ",";
            List<FlowProcessMessageView> flowProcessMessageViewList = new List<FlowProcessMessageView>();
            IQueryable<FlowProcessMessage> flowProcessMessageList = FlowProcessMessageRepository.Entities.Where(f => f.State == 0 && ((f.SponsorUser == userName && f.ProcessType == "1") || (f.ProcessUser == userName && f.ProcessType == "2") || (("," + f.SponsorUser + ",").Contains(contrainUserName) && f.ProcessType == "5")));
            foreach (FlowProcessMessage flowProcessMessage in flowProcessMessageList)
            {
                flowProcessMessageViewList.Add(new FlowProcessMessageView()
                {
                    ID = flowProcessMessage.ID,
                    RecordID = flowProcessMessage.RecordID,
                    FlowCode = flowProcessMessage.FlowCode,
                    FlowNodeCode = flowProcessMessage.FlowNodeCode,
                    ProcessUserDepartment = flowProcessMessage.ProcessUserDepartment,
                    ProcessUser = flowProcessMessage.ProcessUser,
                    SponsorUser = flowProcessMessage.SponsorUser,
                    ProcessType = flowProcessMessage.ProcessType,
                    LastUpdateUser = flowProcessMessage.LastUpdateUser,
                    LastUpdateTime = flowProcessMessage.LastUpdateTime,
                    AddDate = flowProcessMessage.AddDate
                });
            }
            return flowProcessMessageViewList;
        }

        public List<FlowProcessMessageView> GetFlowProcessMessage(string recordID, string userName, string processType)
        {
            List<FlowProcessMessageView> flowProcessMessageViewList = new List<FlowProcessMessageView>();
            IQueryable<FlowProcessMessage> flowProcessMessageList = null;
            if (processType == "1")
            {
                flowProcessMessageList = FlowProcessMessageRepository.Entities.Where(f => f.RecordID == recordID && f.State == 0 && f.SponsorUser == userName && f.ProcessType == "1");
            }
            else
            {
                flowProcessMessageList = FlowProcessMessageRepository.Entities.Where(f => f.RecordID == recordID && f.State == 0 && f.ProcessUser == userName && f.ProcessType == "2");
            }

            foreach (FlowProcessMessage flowProcessMessage in flowProcessMessageList)
            {
                flowProcessMessageViewList.Add(new FlowProcessMessageView()
                {
                    ID = flowProcessMessage.ID,
                    RecordID = flowProcessMessage.RecordID,
                    FlowCode = flowProcessMessage.FlowCode,
                    FlowNodeCode = flowProcessMessage.FlowNodeCode,
                    ProcessUserDepartment = flowProcessMessage.ProcessUserDepartment,
                    ProcessUser = flowProcessMessage.ProcessUser,
                    SponsorUser = flowProcessMessage.SponsorUser,
                    ProcessType = flowProcessMessage.ProcessType,
                    LastUpdateUser = flowProcessMessage.LastUpdateUser,
                    LastUpdateTime = flowProcessMessage.LastUpdateTime,
                    AddDate = flowProcessMessage.AddDate
                });
            }
            return flowProcessMessageViewList;
        }

        public FlowProcessMessageView GetFlowProcessMessageByID(Guid ID)
        {
            FlowProcessMessage flowProcessMessage = FlowProcessMessageRepository.Entities.Single(f => f.ID == ID);
            FlowProcessMessageView flowProcessMessageView = new FlowProcessMessageView();
            flowProcessMessageView.ID = flowProcessMessage.ID;
            flowProcessMessageView.RecordID = flowProcessMessage.RecordID;
            flowProcessMessageView.FlowCode = flowProcessMessage.FlowCode;
            flowProcessMessageView.FlowNodeCode = flowProcessMessage.FlowNodeCode;
            flowProcessMessageView.ProcessUserDepartment = flowProcessMessage.ProcessUserDepartment;
            flowProcessMessageView.ProcessUser = flowProcessMessage.ProcessUser;
            flowProcessMessageView.SponsorUser = flowProcessMessage.SponsorUser;
            flowProcessMessageView.ProcessType = flowProcessMessage.ProcessType;
            flowProcessMessageView.State = flowProcessMessage.State;
            flowProcessMessageView.LastUpdateUser = flowProcessMessage.LastUpdateUser;
            flowProcessMessageView.LastUpdateTime = flowProcessMessage.LastUpdateTime;
            return flowProcessMessageView;
        }

        public void AddFlowProcessMessage(List<FlowProcessMessageView> addList)
        {
            foreach (FlowProcessMessageView flowProcessMessageView in addList)
            {
                FlowProcessMessage flowProcessMessage = new FlowProcessMessage()
                {
                    ID = flowProcessMessageView.ID,
                    RecordID = flowProcessMessageView.RecordID,
                    FlowCode = flowProcessMessageView.FlowCode,
                    FlowNodeCode = flowProcessMessageView.FlowNodeCode,
                    ProcessUserDepartment = flowProcessMessageView.ProcessUserDepartment,
                    ProcessUser = flowProcessMessageView.ProcessUser,
                    SponsorUser = flowProcessMessageView.SponsorUser,
                    ProcessType = flowProcessMessageView.ProcessType,
                    State = 0,
                    LastUpdateUser = flowProcessMessageView.LastUpdateUser,
                    LastUpdateTime = flowProcessMessageView.LastUpdateTime
                };
                FlowProcessMessageRepository.Insert(flowProcessMessage);
            }
        }

        public void UpdateFlowProcessMessage(FlowProcessMessageView flowProcessMessageView)
        {
            FlowProcessMessage flowProcessMessage = FlowProcessMessageRepository.Entities.SingleOrDefault(f => f.ID == flowProcessMessageView.ID);
            if (flowProcessMessage != null)
            {
                flowProcessMessage.ProcessType = flowProcessMessageView.ProcessType;
                flowProcessMessage.ApprovalOpinion = flowProcessMessageView.ApprovalOpinion;
                flowProcessMessage.LastUpdateUser = flowProcessMessageView.LastUpdateUser;
                flowProcessMessage.LastUpdateTime = flowProcessMessageView.LastUpdateTime;
                FlowProcessMessageRepository.Update(flowProcessMessage);
            }
        }

        public void ProcessFlowProcessMessageState(string recordID, string flowNodeCodeStr)
        {
            string sql = @"UPDATE FlowProcessMessages SET State=1 WHERE RecordID='" + recordID + "' AND FlowNodeCode IN (" + flowNodeCodeStr + ") AND ProcessType IN ('1','2','5');";
            FlowProcessMessageRepository.ExeSqlCommon(sql);
        }
    }
}